package week6;

import week6.jsjf.BinaryTreeNode;
import week6.jsjf.LinkedBinaryTree;
import weeek3.jsjf.LinkedQueue;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * The DecisionTree1 class uses the LinkedBinaryTree class to implement
 * a binary decision tree. Tree elements are read from a given file and  
 * then the decision tree can be evaluated based on user input using the
 * evaluate method. 
 * 
 * @author Lewis and Chase
 * @version 4.0
 */
public class DecisionTree1
{
    public  LinkedBinaryTree<String> tree;

    public static void main(String[] args) throws FileNotFoundException {
     DecisionTree1 ab=new DecisionTree1("C:\\Users\\24098\\duan_zhixuan_20172304java\\untitled\\src\\week6\\input.txt");
    BinaryTreeNode a=ab.tree.getRootNode();

        System.out.println( ab.getleavesnumber(a));
        System.out.println(ab.tree.getHeight());
    }
    /**
     * Builds the decision tree based on the contents of the given file
     * @param filename the name of the input file
     * @throws FileNotFoundException if the input file is not found
     */

    int count1=0;
    public DecisionTree1(String filename) throws FileNotFoundException
    {
        File inputFile = new File(filename);
        Scanner scan = new Scanner(inputFile);
        int numberNodes = scan.nextInt();
        scan.nextLine();
        int root = 0, left, right;
        
        List<LinkedBinaryTree<String>> nodes = new ArrayList<LinkedBinaryTree<String>>();
        for (int i = 0; i < numberNodes; i++)
            nodes.add(i,new LinkedBinaryTree<>(scan.nextLine()));
        
        while (scan.hasNext())
        {
            root = scan.nextInt();
            left = scan.nextInt();
            right = scan.nextInt();
            scan.nextLine();
            
            nodes.set(root, new LinkedBinaryTree<>((nodes.get(root)).getRootElement(), nodes.get(left), nodes.get(right)));
        }
        tree = nodes.get(root);
    }

    public int getleavesnumber(BinaryTreeNode node ){

        if(node==null)
        return 0;
        if (node.getLeft()==null&&node.getRight()==null)
            return 1;
        return getleavesnumber(node.getLeft())+getleavesnumber(node.getRight());
    }//计算叶子结点的个数的方法
    public void recursionlevelOrder(BinaryTreeNode Node) {
        if (Node == null) { return; }
        int depth = tree.getHeight();
        for (int i = 1; i <= depth; i++) {
            recursionlevelOrder(Node, i); } }//递归实现层序遍历
    private void recursionlevelOrder(BinaryTreeNode Node, int level) {
        if (Node == null || level < 1) {
            return; }
        if (level == 1) {
            System.out.print(Node.getElement() + "  ");
            return; }
            recursionlevelOrder(Node.getLeft(), level - 1);
        recursionlevelOrder(Node.getRight(), level - 1);
    }//递归实现层序遍历
    LinkedQueue<BinaryTreeNode> B=new LinkedQueue<>();
    public void unrecursionlevelOreder(BinaryTreeNode node) throws InterruptedException {
        B.enqueue(node);
        while(B.size()!=0)
        { BinaryTreeNode a;
            a=B.first();
            System.out.print(a.getElement());
            if (B.first().getLeft()!=null)
                B.enqueue(B.first().getLeft());
            if(B.first().getRight()!=null)
                B.enqueue(B.first().getRight());
            B.dequeue(); } }//非递归实现层序遍历
    /**
     *  Follows the decision tree based on user responses.
     */
      public void evaluate()
    {
        LinkedBinaryTree<String> current = tree;
        Scanner scan = new Scanner(System.in);

        while (current.size() > 1)
        {
             System.out.println (current.getRootElement());
            if (scan.nextLine().equalsIgnoreCase("N"))
                current = current.getLeft();
            else
                current = current.getRight();
        }
        System.out.println ( current.getRootElement());
    }
}